/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.multi.client.account.service.handler.newAccount;

import com.medusa.gruul.common.core.auth.client.helper.ISecurity;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.handler.Handler;
import com.medusa.gruul.common.core.util.SystemCode;
import com.medusa.gruul.multi.client.account.api.entity.MultiClientAccount;
import com.medusa.gruul.multi.client.account.api.entity.MultiClientAccountAuth;
import com.medusa.gruul.multi.client.account.api.entity.MultiClientAccountData;
import com.medusa.gruul.multi.client.account.api.entity.MultiClientAccountShop;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author 张治保
 * date 2021/12/4
 */
public abstract class AbstractNewAccountHandler<T> implements Handler<MultiClientAccount> {
    @Override
    @SuppressWarnings("unchecked")
    @Transactional(rollbackFor = Exception.class)
    public MultiClientAccount handle(Object... params) {
        if (params == null || params.length != 2|| !(params[0] instanceof String)){
            throw new ServiceException(SystemCode.PARAM_BIND_ERROR);
        }
        String userId = (String) params[0];
        return handle(userId,(T) params[1]);
    }

    protected MultiClientAccount handle(String userId,T data){
        MultiClientAccount account = this.newAccount(userId, data);
        MultiClientAccountAuth accountAuth = this.newAuth(account.getId(), userId, data);
        MultiClientAccountData accountData = this.newData(account.getId(), userId, data);
        MultiClientAccountShop accountShop = newAccountShop(account.getId(), userId, data);
        this.other(account.getId(), userId, data);
        ISecurity.updateModifyTimeCache(userId);
        return account.setAuth(accountAuth)
            .setData(accountData)
            .setAccountShop(accountShop);
    }

    /**
     * 新账户
     */
    protected abstract MultiClientAccount newAccount(String userId,T data);

    /**
     * 认证内容
     */
    protected abstract MultiClientAccountAuth newAuth(Long accountId,String userId,T data);

    /**
     * 数据内容
     */
    protected abstract MultiClientAccountData newData(Long accountId,String userId,T data);

    /**
     * 账号店铺关联内容
     */
    protected abstract MultiClientAccountShop newAccountShop(Long accountId,String userId,T data);

    /**
     * 其他内容
     */
    protected abstract void other(Long accountId,String userId,T data);
}
